package com.it.schoolbookshop_back.entities.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.it.schoolbookshop_back.enums.BookConditionEnum;
import com.it.schoolbookshop_back.enums.PriceRangeEnum;
import com.it.schoolbookshop_back.enums.SortByEnum;

/**
 * 书籍查询参数DTO
 * 前端不传的参数则不计入排序和筛选
 */
public class BookQueryDTO {
    @JsonProperty("price_range")
    private PriceRangeEnum priceRange;
    
    @JsonProperty("book_condition")
    private BookConditionEnum bookCondition;
    
    @JsonProperty("sort")
    private SortByEnum sort;
    
    /**
     * 判断价格范围是否被设置
     * @return 如果前端传递了此参数则返回true，否则返回false
     */
    public boolean hasPriceRange() {
        return priceRange != null;
    }
    
    /**
     * 判断书本品相是否被设置
     * @return 如果前端传递了此参数则返回true，否则返回false
     */
    public boolean hasBookCondition() {
        return bookCondition != null;
    }
    
    /**
     * 判断排序方式是否被设置
     * @return 如果前端传递了此参数则返回true，否则返回false
     */
    public boolean hasSort() {
        return sort != null;
    }
    
    /**
     * 获取价格区间的详细信息，用于诊断
     */
    public String getPriceRangeDetails() {
        if (priceRange == null) {
            return "未设置";
        }
        
        String rangeDescription;
        switch (priceRange) {
            case BELOW_20:
                rangeDescription = "20以下 (0-20元)";
                break;
            case RANGE_20_50:
                rangeDescription = "20-50元";
                break;
            case RANGE_50_100:
                rangeDescription = "50-100元";
                break;
            case ABOVE_100:
                rangeDescription = "100以上";
                break;
            default:
                rangeDescription = priceRange.getLabel();
        }
        
        return String.format("%s (%s)", priceRange.name(), rangeDescription);
    }
    
    /**
     * 获取品相的详细信息，用于诊断
     */
    public String getBookConditionDetails() {
        if (bookCondition == null) {
            return "未设置";
        }
        return String.format("%s (label=%s)", 
                bookCondition.name(), 
                bookCondition.getLabel());
    }
    
    /**
     * 获取排序方式的详细信息，用于诊断
     */
    public String getSortDetails() {
        if (sort == null) {
            return "未设置";
        }
        return String.format("%s (label=%s)", 
                sort.name(), 
                sort.getLabel());
    }
    
    public PriceRangeEnum getPriceRange() {
        return priceRange;
    }
    
    public void setPriceRange(PriceRangeEnum priceRange) {
        this.priceRange = priceRange;
    }
    
    public BookConditionEnum getBookCondition() {
        return bookCondition;
    }
    
    public void setBookCondition(BookConditionEnum bookCondition) {
        this.bookCondition = bookCondition;
    }
    
    public SortByEnum getSort() {
        return sort;
    }
    
    public void setSort(SortByEnum sort) {
        this.sort = sort;
    }
    
    @Override
    public String toString() {
        return "BookQueryDTO{" +
                "priceRange=" + getPriceRangeDetails() +
                ", bookCondition=" + getBookConditionDetails() +
                ", sort=" + getSortDetails() +
                '}';
    }
} 